package linc.cool.listen;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * @author yqlin
 * @date 2022/3/4 3:12 上午
 * @description
 */
@Slf4j
@Component
public class AckListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        // 获取消息id
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 获取消息
        log.info(new String(message.getBody()));
        // 进行业务处理
        log.info("进行业务处理");
        // 模拟出现异常
        try {
           // int i = 5 / 0;
            // 完成签收
            channel.basicAck(deliveryTag, true);
        } catch (Exception e) {
            // 拒绝签收
             /*
                 第三个参数：requeue：重回队列。如果设置为true，则消息重新回到queue，broker会重新发送该消息给消费端
             */
            channel.basicNack(deliveryTag, true, true);
        }


    }

}
